package com.example.dell.jakatra.retrofit;


import com.example.dell.jakatra.BuildConfig;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * Created by johnvi on 2016/12/20.
 * mail:229200348@qq.com
 */
public class RetrofitManager {
    private volatile static RetrofitManager INSTANCE;
    private Retrofit retrofit;

    private RetrofitManager() {
        initRetorfit();
    }


    private void initRetorfit() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (BuildConfig.DEBUG) {
            //拦截器
            HttpLoggingInterceptor LoginInterceptor = new HttpLoggingInterceptor();
            LoginInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            builder.addInterceptor(LoginInterceptor); //添加retrofit日志打印
        }
        //

        builder.connectTimeout(2 * 60, TimeUnit.SECONDS);
        builder.readTimeout(20, TimeUnit.SECONDS);
        builder.writeTimeout(20, TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(true);
        OkHttpClient client = builder.build();


        retrofit = new Retrofit
                .Builder()
                .baseUrl("http://zf.poopootoo.com/")
                .addConverterFactory(MyGsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//这个就是用来适配RxJava的
                .client(client)
                .build();
    }

    public <T> T createApi(Class<T> tClass) {
        return retrofit.create(tClass);
    }


    public static RetrofitManager getInstance() {
        if (INSTANCE == null) {
            synchronized (RetrofitManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new RetrofitManager();
                }
            }
        }
        return INSTANCE;
    }



}
